package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IFreelanceAlertDAO;
import org.insia.smartposse.metier.FreelanceAlert;
import org.insia.smartposse.metier.webservice.LightFreelanceAlert;

/**
 * DAO for FreelanceAlert class based on GenericDAO.
 * @author sok hout
 *
 */
public class FreelanceAlertDAO 
extends GenericDAO<FreelanceAlert, Long> 
implements IFreelanceAlertDAO {
	/**
	 * Empty constructor.
	 *
	 */
	public FreelanceAlertDAO() {
		type = FreelanceAlert.class;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceAlertDAO#createLightFreelanceAlert(org.insia.smartposse.metier.webservice.LightFreelanceAlert)
	 */
	public LightFreelanceAlert createLightFreelanceAlert(LightFreelanceAlert lfalert) {
		try {
				FreelanceAlert falert = new FreelanceAlert();
				falert.setFreelance(DAOFactory.getFreelanceDAO().get(lfalert.getFreelance()));
				falert.setSkill(DAOFactory.getSkillDAO().get(lfalert.getSkill()));
				lfalert.setId(DAOFactory.getFreelanceAlertDAO().create(falert));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lfalert;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceAlertDAO#createLightFreelanceAlert(java.util.List)
	 */
	public List<LightFreelanceAlert> createLightFreelanceAlert(List<LightFreelanceAlert> llfalert) {
		List<LightFreelanceAlert> LlfAlert = new ArrayList<LightFreelanceAlert>();
		try {
			for (LightFreelanceAlert lfalert : llfalert) {
				lfalert = createLightFreelanceAlert(lfalert);
				LlfAlert.add(lfalert);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfAlert;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceAlertDAO#updateLightFreelanceAlert(org.insia.smartposse.metier.webservice.LightFreelanceAlert)
	 */
	public LightFreelanceAlert updateLightFreelanceAlert(LightFreelanceAlert lfalert) {
		try {
			FreelanceAlert falert = DAOFactory.getFreelanceAlertDAO().get(lfalert.getId());
			falert.setFreelance(DAOFactory.getFreelanceDAO().get(lfalert.getFreelance()));
			falert.setSkill(DAOFactory.getSkillDAO().get(lfalert.getSkill()));
			DAOFactory.getFreelanceAlertDAO().update(falert);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lfalert;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceAlertDAO#updateLightFreelanceAlert(java.util.List)
	 */
	public List<LightFreelanceAlert> updateLightFreelanceAlert(List<LightFreelanceAlert> llfalert) {
		List<LightFreelanceAlert> LlfAlert = new ArrayList<LightFreelanceAlert>();
		try {
			for (LightFreelanceAlert lfalert : llfalert) {
				lfalert = updateLightFreelanceAlert(lfalert);
				LlfAlert.add(lfalert);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfAlert;
	}
}
